"""
Mengji 错误
Mengji 使用错误进行稳定的重入，这里只有一类错误，使用成员函数和属性判断具体错误。
只有 EXIT_ERROR 错误会让系统完全退出，其他错误被认为 *在系统运行中可能通过重启解决*
MJ 框架会尝试重新初始化载具以解决问题。

date: 2025/3/19
author: SiHeng Tang
file: mj_errors.py
copyright(c) DFSA Eastern Software
此程序不能提供任何担保 WITHOUT WARRANTY OF ANY KIND
"""

NORMAL_ENTRY_ERROR_MSG = "System normal start running."
EXIT_ERROR_MSG = "System exit."
INTERNAL_ERROR_MSG = "Some framework internal error happened."
PROCESSOR_ERROR_MSG = "Some error happened in unit processor."
VBUS_ERROR_MSG = "Some error happened in virtual bus."


class MJRuntimeError(Exception):
    # 运行期异常错误
    INTERNAL_ERROR = 1
    PROCESSOR_ERROR = 2
    VBUS_ERROR = 3
    NORMAL_ENTRY_ERROR = 4

    # api 处理流程错误
    RESTART_MENGJI_MAIN = 5

    EXIT_ERROR = 0

    def __init__(self, code, msg):
        super().__init__()
        self.code = code
        self.msg = msg

    def __str__(self):
        """
        字符串转换包含了 ``错误代码`` ``提示消息``
        """
        common_msg = ""

        if self.code == self.NORMAL_ENTRY_ERROR:
            common_msg = NORMAL_ENTRY_ERROR_MSG
        elif self.code == self.EXIT_ERROR:
            common_msg = EXIT_ERROR_MSG
        elif self.code == self.INTERNAL_ERROR:
            common_msg = INTERNAL_ERROR_MSG
        elif self.code == self.PROCESSOR_ERROR:
            common_msg = PROCESSOR_ERROR_MSG
        elif self.code == self.VBUS_ERROR:
            common_msg = VBUS_ERROR_MSG

        return f"{common_msg} [{self.code=}] {self.msg}"

    def get_err_code(self):
        return self.code

    def is_the_err(self, kind):
        return kind == self.code
